package com.windsea.relationshipmining.services;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.text.format.Time;
import android.util.Log;

import com.windsea.relationshipmining.database.DBServiceHandler;

/* This class collects restarts all Service at bootup
 * if lifetime is not exceeded */

public class BootUpReceiver extends BroadcastReceiver {

//	private static final String TAG = BootUpReceiver.class.getSimpleName();

	@Override
	public void onReceive(Context context, Intent intent) {

		// Do something after Bootup is completed
		Time n = new Time();
		n.setToNow();
		long now = n.toMillis(false);

		DBServiceHandler sHandler = new DBServiceHandler(context);
		sHandler.open();

		Cursor LT = sHandler.getLifeTime();
		LT.moveToFirst();
		long end = Long.parseLong(LT.getString(LT.getColumnIndex("LifeTime")));

		// Start all periodic services after bootup
		if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)
				&& end > now) {
//			Log.d(TAG, "Phone just booted up");

			/* Here we need all the Services again, so we need to declare them */
			Intent watcherService;

			// Start WatcherService //
			watcherService = new Intent(context, WatcherService.class);
			context.startService(watcherService);

//			Log.d(TAG, "Started Service again");
		}
		sHandler.close();
	}

}